
$(function () {
 
    var t = $("#logTableList").bootstrapTable({
        url: constUrl + 'rest/v1/job/page',
        method: 'get',
        dataType: "json",
        striped: true,//设置为 true 会有隔行变色效果
        undefinedText: "",//当数据为 undefined 时显示的字符
        pagination: true, //分页
        // paginationLoop:true,//设置为 true 启用分页条无限循环的功能。
//        showToggle: "false",//是否显示 切换试图（table/card）按钮
//        showColumns: "false",//是否显示 内容列下拉框
        pageNumber: 1,//如果设置了分页，首页页码
        sortable:true,
        sortorder:"asc",
        // showPaginationSwitch:true,//是否显示 数据条数选择框
        pageSize: 10,//如果设置了分页，页面数据条数
        pageList: [ 10, 20, 50],	//如果设置了分页，设置可供选择的页面数据条数。设置为All 则显示所有记录。
        paginationPreText: '‹',//指定分页条中上一页按钮的图标或文字,这里是<
        paginationNextText: '›',//指定分页条中下一页按钮的图标或文字,这里是>
        // singleSelect: false,//设置True 将禁止多选
        search: true, //显示搜索框
        data_local: "zh-CN",//表格汉化
        sidePagination: "server", //服务端处理分页
        queryParams: function (params) {//自定义参数，这里的参数是传给后台的，我这是是分页用的
        	//alert(params.sort +"  " + params.order)
            return {//这里的params是table提供的
                currentPage: params.offset/params.limit + 1,//从数据库第几条记录开始
                pageSize: params.limit,//找多少条
                searchText: params.search,
                sort:params.sort,
                sortOrder:params.order
            };
        },
        idField: "jobId",//指定主键列
        columns: [
            {
                title: '作业ID',//表的列名
                field: 'jobId',//json数据中rows数组中的属性名
                sortable:true,
                align: 'center'//水平居中
            },
            {
                title: '作业名',
                field: 'jobName',
                sortable:true,
                align: 'center'
            },
            {
                title: '执行节点',
                field: 'executeHost',
                sortable:true,
                align: 'center',
                formatter:function(value,row,index){
                	if(row.executeHost==undefined || row.executeHost=='')
                		return undefined;
                	return "<a href=\"javascript:void(0)\" onclick=\"openNodeDetail('"+row.executeHost+"')\" >"+row.executeHost+"</a>";
                	}
            },
            {
                //EMAIL
                title: 'CPU使用率',
                field: 'cpuUsage',
                align: 'center',
                formatter:function(value,row,index){
                	if(row.cpuUsage==undefined || row.cpuUsage=='')
                		return undefined;
                	return value;
                	}
                	
            },
            {
                //部门名字
                title: '内存使用量',
                field: 'usedMemory',//可以直接取到属性里面的属性，赞
                align: 'center'
                	
            },
            {
                title: '日志记录',
                field: '',
                align: 'center',
                formatter: function (value, row, index) {//自定义显示，这三个参数分别是：value该行的属性，row该行记录，index该行下标
                	 return "<div class=\"am-btn-group am-btn-group-xs\">" +
                	 			"<div type=\"button\" class=\"am-btn am-btn-default am-radius\" onclick='readWLog(\""+ row.jobId +"\")'>调度日志</div>" +
                	 				"</div>";
                }
 
            },{
                title: '状态',
                field: 'jobStatus',//可以直接取到属性里面的属性，赞
                sortable:true,
                align: 'center',
                formatter:status
            },
            {
                title: '操作',
                field: '',
                align: 'center',
                formatter: operators
            }
 
        ]
    });
 
 
    t.on('load-success.bs.table', function (data) {//table加载成功后的监听函数
        console.log("load success");
        $(".pull-right").css("display", "block");
    });
 
});
function status(value,row,index){
	if (row.jobStatus=="DONE"){
        html = '<div class="am-alert am-alert-success my-state-css">DONE</div>';
    } else if (row.jobStatus=="RUNNING"){
        html = '<div class="am-alert am-alert-success my-state-css">RUNNING</div>';
    } else if (row.jobStatus=="PAUSE"){
        html = '<div class="am-alert am-alert-secondary my-state-css">PAUSE</div>';
    } else if (row.jobStatus=="ERROR"){
        html = '<div class="am-alert am-alert-danger my-state-css">ERROR</div>';
    } else if (row.jobStatus=="STOP"){
        html = '<div class="am-alert am-alert-warning my-state-css">STOPPING</div>';
    } else if (row.jobStatus=="EXIT"){
        html = '<div class="am-alert am-alert-warning my-state-css">EXIT</div>';
    } else {
        html= '状态查询出错';
    }
	return html;
}
function operators(value, row, index){
	var con = "<div class=\"am-btn-group am-btn-group-xs\">" +
    "<div type=\"button\" class=\"am-btn am-btn-default am-radius\" onclick=\"openWorkDetail('"+row.jobId+"')\">详情</div>" +
    "<div type=\"button\" class=\"am-btn am-btn-default am-radius\" onclick=\"exportWork('"+row.jobId+"')\">导出</div>" ;
	if (row.jobStatus=="DONE"){
        con += "<div type=\"button\" class=\"am-btn am-btn-default am-radius\" onclick=\"stopJob('"+row.jobId+"')\" disabled>挂起</div>" +
            "<div type=\"button\" class=\"am-btn am-btn-default am-radius\" onclick=\"killJob('"+row.jobId+"')\" disabled>终止</div>" +
            "<div type=\"button\" class=\"am-btn am-btn-default am-radius\" onclick=\"topJob('"+row.jobId+"')\" disabled>置顶</div>" +
            "<div type=\"button\" class=\"am-btn am-btn-default am-radius\" onclick=\"botJob('"+row.jobId+"')\" disabled>置底</div>" +
           /* "<div type=\"button\" class=\"am-btn am-btn-default am-radius\">修改</div>" +*/
            "<div type=\"button\" class=\"am-btn am-btn-default am-radius\" onclick=\"infodelete('"+row.jobId+"')\">删除</div>"/**/;
    } else if (row.jobStatus=="RUNNING"){
        con += "<div type=\"button\" class=\"am-btn am-btn-default am-radius\" onclick=\"stopJob('"+row.jobId+"')\">挂起</div>"+
            "<div type=\"button\" class=\"am-btn am-btn-default am-radius\" onclick=\"killJob('"+row.jobId+"')\">终止</div>" +
            "<div type=\"button\" class=\"am-btn am-btn-default am-radius\" onclick=\"topJob('"+row.jobId+"')\">置顶</div>" +
            "<div type=\"button\" class=\"am-btn am-btn-default am-radius\" onclick=\"botJob('"+row.jobId+"')\">置底</div>" +
           /* "<div type=\"button\" class=\"am-btn am-btn-default am-radius\">修改</div>" +*/
            "<div type=\"button\" class=\"am-btn am-btn-default am-radius\" onclick=\"infodelete('"+row.jobId+"')\">删除</div>"/**/;
    } else if (row.jobStatus=="PAUSE"){
        con += "<div type=\"button\" class=\"am-btn am-btn-default am-radius\" onclick=\"resumeJob('"+row.jobId+"')\">恢复</div>" +
            "<div type=\"button\" class=\"am-btn am-btn-default am-radius\" onclick=\"killJob('"+row.jobId+"')\">终止</div>" +
            "<div type=\"button\" class=\"am-btn am-btn-default am-radius\" onclick=\"topJob('"+row.jobId+"')\">置顶</div>" +
            "<div type=\"button\" class=\"am-btn am-btn-default am-radius\" onclick=\"botJob('"+row.jobId+"')\">置底</div>" +
           /* "<div type=\"button\" class=\"am-btn am-btn-default am-radius\">修改</div>" +*/
            "<div type=\"button\" class=\"am-btn am-btn-default am-radius\" onclick=\"infodelete('"+row.jobId+"')\">删除</div>"/**/;
    } else if (row.jobStatus=="ERROR"){
        con += "<div type=\"button\" class=\"am-btn am-btn-default am-radius\" onclick=\"resumeJob('"+row.jobId+"')\" disabled>恢复</div>" +
            "<div type=\"button\" class=\"am-btn am-btn-default am-radius\" onclick=\"killJob('"+row.jobId+"')\" disabled>终止</div>" +
            "<div type=\"button\" class=\"am-btn am-btn-default am-radius\" onclick=\"topJob('"+row.jobId+"')\">置顶</div>" +
            "<div type=\"button\" class=\"am-btn am-btn-default am-radius\" onclick=\"botJob('"+row.jobId+"')\">置底</div>" +
          /*  "<div type=\"button\" class=\"am-btn am-btn-default am-radius\">修改</div>" +*/
            "<div type=\"button\" class=\"am-btn am-btn-default am-radius\" onclick=\"infodelete('"+row.jobId+"')\">删除</div>"/**/;
    } else if (row.jobStatus=="STOP"){
        con += "<div type=\"button\" class=\"am-btn am-btn-default am-radius\" onclick=\"resumeJob('"+row.jobId+"')\" disabled>恢复</div>" +
            "<div type=\"button\" class=\"am-btn am-btn-default am-radius\" onclick=\"killJob('"+row.jobId+"')\" disabled>终止</div>" +
            "<div type=\"button\" class=\"am-btn am-btn-default am-radius\" onclick=\"topJob('"+row.jobId+"')\">置顶</div>" +
            "<div type=\"button\" class=\"am-btn am-btn-default am-radius\" onclick=\"botJob('"+row.jobId+"')\">置底</div>" +
           /* "<div type=\"button\" class=\"am-btn am-btn-default am-radius\">修改</div>" +*/
            "<div type=\"button\" class=\"am-btn am-btn-default am-radius\" onclick=\"infodelete('"+row.jobId+"')\">删除</div>"/**/;
    } else {
        //con += '状态查询出错';
    }
	con+=            "</div>";
	return con;
}

function openNodeDetail(id) {
    window.location.href=constUrl+"views/node/detail.action?nodeId="+id;
}

//删除列表内容
function infodelete(workId){
    var index = parent.layer.getFrameIndex(window.name);
    layer.confirm('确定要删除该作业吗？', {
        btn: ['确定','取消'] //按钮
    }, function(){
        $.ajax({
            type:"POST",
            url:constUrl+"JobServlet",
            dataType:"json",
            data: {
                operateType: "delete",
                jobId: workId
            },
            success:function(data){
                layer.confirm('作业删除成功！',{
                    btn:['确定']
                },function () {
                    reloadList();
                });
            },
            error:function () {
                layer.msg('作业删除失败');
            }
        });
    }, function(){
        layer.msg('取消删除作业', {
            time: 1000 //1s后自动关闭
        });
    });
}

function stopJob(workId) {
    layer.confirm('确定要挂起该作业吗？', {
        btn: ['确定','取消'] //按钮
    }, function(){
        $.ajax({
            type:"POST",
            url:constUrl+"JobServlet",
            dataType:"json",
            data: {
                operateType: "stop",
                jobId: workId
            },
            success:function(data){
                layer.confirm('作业挂起成功！',{
                    btn:['确定']
                },function () {
                	reloadList();
                });
            },
            error:function () {
                layer.msg('作业挂起失败');
            }
        });
    }, function(){
        layer.msg('取消挂起作业', {
            time: 1000 //1s后自动关闭
        });
    });
}

function resumeJob(workId) {
    layer.confirm('确定要恢复该作业吗？', {
        btn: ['确定','取消'] //按钮
    }, function(){
        $.ajax({
            type:"POST",
            url:constUrl+"JobServlet",
            dataType:"json",
            data: {
                operateType: "resume",
                jobId: workId
            },
            success:function(data){
                layer.confirm('作业恢复成功！',{
                    btn:['确定']
                },function () {
                	reloadList();
                });
            },
            error:function () {
                layer.msg('作业恢复失败');
            }
        });
    }, function(){
        layer.msg('取消恢复作业', {
            time: 1000 //1s后自动关闭
        });
    });
}

function killJob(workId) {
    layer.confirm('确定要终止该作业吗？', {
        btn: ['确定','取消'] //按钮
    }, function(){
        $.ajax({
            type:"POST",
            url:constUrl+"JobServlet",
            dataType:"json",
            data: {
                operateType: "kill",
                jobId: workId
            },
            success:function(data){
                layer.confirm('作业终止成功！',{
                    btn:['确定']
                },function () {
                	reloadList();
                });
            },
            error:function () {
                layer.msg('作业置顶失败');
            }
        });
    }, function(){
        layer.msg('取消置顶作业', {
            time: 1000 //1s后自动关闭
        });
    });
}

function topJob(workId) {
    layer.confirm('确定要置顶该作业吗？', {
        btn: ['确定','取消'] //按钮
    }, function(){
        $.ajax({
            type:"POST",
            url:constUrl+"JobServlet",
            dataType:"json",
            data: {
                operateType: "top",
                jobId: workId
            },
            success:function(data){
                layer.confirm('作业置顶成功！',{
                    btn:['确定']
                },function () {
                	reloadList();
                });
            },
            error:function () {
                layer.msg('作业置顶失败');
            }
        });
    }, function(){
        layer.msg('取消置顶作业', {
            time: 1000 //1s后自动关闭
        });
    });
}

function botJob(workId) {
    layer.confirm('确定要置底该作业吗？', {
        btn: ['确定','取消'] //按钮
    }, function(){
        $.ajax({
            type:"POST",
            url:constUrl+"JobServlet",
            dataType:"json",
            data: {
                operateType: "bot",
                jobId: workId
            },
            success:function(data){
                layer.confirm('作业置底成功！',{
                    btn:['确定']
                },function () {
                	reloadList();
                });
            },
            error:function () {
                layer.msg('作业置底失败')
            }
        });
    }, function(){
        layer.msg('取消置底作业', {
            time: 1000 //1s后自动关闭
        });
    });
}

function openWorkDetail(id) {
	window.location.href=constUrl+"views/job/detail.action?workId="+id;
}

//导出文件
function exportWork(id) {
    window.open(constUrl+"ws/job/GetJobXML?jobId="+id);
}


function readWLog(workId){
    layer.open({
        type: 2,
        skin: 'layui-layer-lan',
        title: '日志查看',
        fix: false,
        scrollbar: false,
        shadeClose: true,
        maxmin: true,
        area: ['500px', '750px'],
        content: constUrl+"views/log/detail.action?url="+workId+".log"
    });
}
function reloadList(){
    window.location.href=constUrl+"views/job/list.action";
}
